package com.whsxt.sys.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;
import com.whsxt.sys.data.MenuData;
import com.whsxt.sys.data.PermissionData;
import com.whsxt.sys.domain.Permission;
import com.whsxt.sys.service.IPermissionService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-21
 */
@RestController
@RequestMapping("/sys/permission")
public class PermissionController {

	@Autowired
	private IPermissionService iPermissionService;
	
	/**************菜单*****************/
	/**
	 * 菜单树行结构
	 */
	@RequestMapping("/dtree")
	public Object dtree() {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("type", ConstantUtil.TYPE_MENU);
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		List<Permission> list = iPermissionService.list(queryWrapper );
		List<MenuData> datas = new ArrayList<MenuData>();
		for (Permission permission : list) {
			Boolean spread = permission.getOpen()==1?true:false;
			datas.add(new MenuData(permission.getId(), permission.getPid(), permission.getTitle(), spread ));
		}
		return new Result(datas);
	}
	/**
	 *  权限列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(PermissionData data) {
		IPage<Permission> page = new Page<Permission>(data.getPage(),data.getLimit());
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("type", ConstantUtil.TYPE_PERMISSION);
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		queryWrapper.like(StrUtil.isNotBlank(data.getTitle()), "title", data.getTitle());
		queryWrapper.like(StrUtil.isNotBlank(data.getPercode()), "percode", data.getPercode());
		queryWrapper.and(data.getId() !=null, 
				i -> 
				i.eq("id", data.getId()).or().eq("pid", data.getId()));
		queryWrapper.orderByAsc("ordernum");
		iPermissionService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	
	/**
	 * 获取最大的排序码
	 * @return
	 */
	@RequestMapping("/getOrdernum")
	public Map<String, Object> getOrdernum() {
		Map<String, Object> map = new HashMap<String, Object>();
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.orderByDesc("ordernum");
		IPage<Permission> page = new Page<Permission>(1, 1);
		iPermissionService.page(page, queryWrapper);
		List<Permission> records = page.getRecords();
		if(records.size()>0) {
			map.put("value",records.get(0).getOrdernum()+1);
		}else {
			map.put("value", 1);
		}
		return map;
	}
	/**
	 * 权限新增
	 * @param data
	 * @return
	 */
	@RequestMapping("/addPermission")
	public Object addPermission(PermissionData data) {
		try {
			data.setType(ConstantUtil.TYPE_PERMISSION);
			iPermissionService.save(data);
			return new Result(CodeMsg.PERMISSION_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PERMISSION_ADD_ERROR);
		}
	}
	/**
	 * 更新权限数据
	 * @param data
	 * @return
	 */
	@RequestMapping("/updatePermission")
	public Object updatePermission(PermissionData data) {
		try {
			iPermissionService.updateById(data);
			return new Result(CodeMsg.PERMISSION_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PERMISSION_UPDATE_ERROE);
		}
	}
	/**
	 * 判断删除的节点是否有子节点
	 * @param id
	 * @return
	 */
	@RequestMapping("/ishaschild")
	public Map<String, Object> ishaschild(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("pid", id);
		int count = iPermissionService.count(queryWrapper);
		if(count != 0) {
			map.put("value", true);
		}else {
			map.put("value", false);
		}
		return map;
	}
	/**
	 * 删除单个数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iPermissionService.removeById(id);
			return new Result(CodeMsg.PERMISSION_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.PERMISSION_DELETE_ERROE);
		}
	}
	
}

